package com.qf.driver.api;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qf.common.bean.R;
import com.qf.common.dto.CmdParamDto;
import com.qf.driver.service.CmdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 接收指令下发到设备并执行
 * @author 千锋健哥
 */
@RestController
@RequestMapping("/driver/cmd")
public class CmdController {

    @Autowired
    private CmdService cmdService;

    /**
     * 接收发送的执行并执行
     * @param dto
     * @return
     */
    @PostMapping("/send")
    public R<String> cmdSend(@RequestBody(required = true) CmdParamDto dto) {
        if (StringUtils.isEmpty(dto.getCmdName())
                || StringUtils.isEmpty(dto.getDeviceKey())
                || StringUtils.isEmpty(dto.getValue())) {
            return R.fail("设备key, 指令名称, 指令值都不可以为空!");
        }
        Boolean flag = cmdService.write(dto);
        if (flag) {
            return R.ok("指令执行成功!");
        } else {
            return R.fail("执行执行失败!");
        }

    }
}
